package com.gym.code.generator.impl.bean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;

import com.gym.code.generator.Contants;
import com.gym.code.generator.EnumContants.AccessModifierType;
import com.gym.code.generator.EnumContants.FieldClassType;
import com.gym.code.generator.impl.degelate.method.BeanMethodGenerateHandler;
import com.gym.code.generator.impl.degelate.method.GetMethodGenerateHandlerImpl;
import com.gym.code.generator.impl.degelate.method.SetMethodGenerateHandlerImpl;
import com.gym.code.generator.impl.enumerate.EnumContentGenerator;
import com.gym.code.generator.impl.enumerate.EnumFileGenerator;
import com.gym.code.generator.model.mapping.bean.BeanGenerateModel;
import com.gym.code.generator.model.mapping.bean.EnumGenerateModel;
import com.gym.code.generator.model.mapping.bean.EnumGenerateModel.EnumScope;
import com.gym.code.generator.model.mapping.field.EnumFieldGenerateModel;
import com.gym.code.generator.model.mapping.field.FieldGenerateModel;
import com.gym.code.generator.model.mapping.field.ParameterListValueFieldGenerateModel;

/**
 * 根据表的相关信息生成javabean信息（有返回结果）
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-4-12 上午11:30:39
 */
public class BeanContentGenerator extends AbstractBeanGenerator{

	private BeanContentGenerator(){}
	
	private static BeanContentGenerator instance = new BeanContentGenerator();
	
	public static BeanContentGenerator getInstance(){
		return instance;
	}
	
	private BeanMethodGenerateHandler getMethod = GetMethodGenerateHandlerImpl.getInstance();
	private BeanMethodGenerateHandler setMethod = SetMethodGenerateHandlerImpl.getInstance();
	private EnumContentGenerator enumGeneratorHandler = EnumContentGenerator.getInstance();
	private EnumFileGenerator enumFileGenerator = EnumFileGenerator.getInstance();
	
	@Override
	protected Set<String> doGenerateImport(BeanGenerateModel bean) {
		return generateFieldImport(bean.getColumnProperyMappingList());
	}

	
	
	protected final Set<String> generateFieldImport(List<FieldGenerateModel> params){
		Set<String> result = new HashSet<String>();
		
		for(FieldGenerateModel item : params){
			if(item instanceof EnumFieldGenerateModel) {
				EnumGenerateModel enumGenerateModel = ((EnumFieldGenerateModel)item).getEnumGenerateModel();
				if(enumGenerateModel.getScope() == EnumScope.global){
					result.add(enumGenerateModel.getPackagePath() +"."+enumGenerateModel.getEnumTypeNameByFirstLetterUpper());
				}
			}else if(item instanceof ParameterListValueFieldGenerateModel){
				result.add("java.util.List");
			} else if(FieldClassType.date == item.getValueType() || FieldClassType.datetime == item.getValueType()){
				result.add("java.util.Date");
			}
		}
		return result;
	
	}
	
	@Override
	protected List<String> generateClassBegin(BeanGenerateModel bean) {
		return Arrays.asList("public class " + bean.getBeanName() + " {");
	}


	@Override
	protected List<String> doGenerateFields(List<FieldGenerateModel> columnProperyMappingList) {
		List<String> result = new ArrayList<String>();
		for(FieldGenerateModel item : columnProperyMappingList) {
			String fieldTypeName = "";
			if(FieldClassType.isNumerType(item.getValueType())){
				fieldTypeName = item.getValueType().getName();
			} else {
				fieldTypeName = item.getValueType().getJavaName();
			}
			String propertyStr = generatePropertyLine(AccessModifierType.$private,fieldTypeName,item.getPropertyName());
			result.add(propertyStr);
		}
		return result;
	}

	@Override
	protected List<String> doGenerateMethods(List<FieldGenerateModel> columnProperyMappingList) {
		List<String> result = new ArrayList<String>();
		for(FieldGenerateModel model : columnProperyMappingList){
			result.addAll(getMethod.generateMethod(model));
			result.addAll(setMethod.generateMethod(model));
		}
		return result;
	}

	@Override
	protected List<String> doGenerateInnerClass(List<EnumGenerateModel> enumModelList) {
		if(CollectionUtils.isEmpty(enumModelList)){
			return Collections.emptyList();
		}
		List<String> result = new ArrayList<String>();
		for(EnumGenerateModel item : enumModelList) {
			if(EnumScope.global == item.getScope()){
				enumFileGenerator.generate(item);
			} else {
				result.addAll(enumGeneratorHandler.generate(item));
			}
		}
		return result;
	}



	@Override
	protected String generatePackage(BeanGenerateModel bean) {
		return "package " + bean.getPackageName() + Contants.$lineSplit;
	}

}
